import {LinkList} from './linklist.js'

// 1. 两个有序链表 合并成一个新链表
// 1->3->5  2->4->6
// 1->2->3->4->5->6
// function combine(ll1, ll2) {
//   const newLL = new LinkList()
//   let point1 = ll1.dummy.next
//   let point2 = ll2.dummy.next

//   while(point1 || point2) {
//     if (!point1) {
//       newLL.addAtTail(point2.val)
//       point2 = point2.next
//       continue
//     }

//     if (!point2) {
//       newLL.addAtTail(point1.val)
//       point1 = point1.next
//       continue
//     }

//     if (point1.val <= point2.val ) {
//       newLL.addAtTail(point1.val)
//       point1 = point1.next
//     } else {
//       newLL.addAtTail(point2.val)
//       point2 = point2.next
//     }
//   }
  
//   return newLL
// }

// // 一共有k个链表的数组
// function combineArr(arr) {
//   let arrFor = Array.from(arr)
//   let length = arrFor.length

//   while(arrFor.length) {
//     if (arrFor.length === 1) {
//       return combine(arrFor[0])
//     } else {
//     }
//   }
// }

// // 合并链表(先合并ll1, 后ll2。 ll1, ll2的指针一起运动。) 
// function combine2(ll1, ll2) {
//   const newLL = new LinkList()
//   let point1 = ll1.dummy.next
//   let point2 = ll2.dummy.next

//   while(point1 || point2) {
//     if (!point1) {
//       newLL.addAtTail(point2.val)
//       point2 = point2.next
//       continue
//     }

//     if (!point2) {
//       newLL.addAtTail(point1.val)
//       point1 = point1.next
//       continue
//     }

//     if (point1 && point2) {
//       newLL.addAtTail(point1.val)
//       newLL.addAtTail(point2.val)
//       point1 = point1.next
//       point2 = point2.next
//     }
//   }

//   return newLL
// }

// // 反转相邻节点
// function reverseNear(ll) {
//   const newLL1 = new LinkList()
//   const newLL2 = new LinkList()

//   let point = ll.dummy.next
//   let i = 1
//   while(point) {
//     if (i % 2 === 1) newLL1.addAtTail(point.val)
//     else newLL2.addAtTail(point.val)
//     i++
//     point = point.next
//   }

//   return combine2(newLL2, newLL1)
// }

// 
function reverseByK(ll, k) {
  if (ll.length < k) return ll
  const llArr = []

  for (let i = 0; i < k; i++) {
    const ll = new LinkList()
    llArr.push(ll)
  }
  
  let point = ll.dummy.next
  let cursor = 1
  
  let limit = ll.length - (ll.length % k)
  while(cursor <= limit  ) {
    llArr[(cursor - 1) % k ].addAtTail(point.val)
    point = point.next
    cursor++
  }


  const newLL = new LinkList()
  for (let j = 0; j < llArr[0].length; j++) {
      for (let i = (llArr.length - 1); i >= 0; i--) {
      newLL.addAtTail(llArr[i].get(j))
    }
  }

  let newLength = newLL.length
  for (let i = newLength ; i < ll.length; i++) {
    newLL.addAtTail(ll.get(i))
  }

  return newLL
}

function reverseByKSim(ll, k) {
  const con = new LinkList()
  const res = new LinkList()

  let point = ll.dummy.next
  while(point) {
    // if (point.val === 7)  debugger
    con.addAtTail(point.val)
    if (con.length % 3 === 0 && con.length) {
      for (let i = k - 1; i >= 0 ; i--) {
        res.addAtTail(con.get(i))
      }

      con.clear()
      point = point.next
      continue
    }


    
    point = point.next

  }

  if (con.length) {
    for (let i = 0; i < con.length; i++) {
      res.addAtTail(con.get(i))
    }
  }

  return res
}

// 
function deleteByIndexK(ll, k) {
  let front = ll.dummy.next
  let back = front
  let step = 0
  while(step < k && front && front.next) {
    front = front.next
    step++
  }

  while(front && front.next) {
    front = front.next
    back = back.next
  }

  if(step === k) {
    back.next = back.next.next
  }
  
  return ll
} 


const ll = new LinkList()
const ll1 = new LinkList()

ll.addAtTail(1)
ll.addAtTail(3)
ll.addAtTail(5)
ll.addAtTail(7)
ll.addAtTail(9)
ll.addAtTail(11)
ll1.addAtTail(2)
ll1.addAtTail(4)
ll1.addAtTail(6)
ll1.addAtTail(8)
ll1.addAtTail(10)

console.log(reverseByK(ll, 3));
// console.log(deleteByIndexK(ll, 1));

console.log(reverseByKSim(ll, 3));
// const newLL = combine(ll, ll1)
// console.log(newLL);
// console.log(reverseNear(newLL));
